import random
import ctypes
import time
import os


def getcount(path):    # 统计.jpg文件个数函数
    count=0
    g = os.walk(path)
    for path,d,filelist in g:
        for filename in filelist:
            if filename.endswith('jpg'):
                print (os.path.join(path, filename))
                count+=1
                return count

def main():
    path = "C:\\Users\\骑士20\\Pictures\\壁纸\\"   #壁纸所在目录
    sum_count = getcount(path)  #统计壁纸文件下图片的个数
    while True:
        n = random.randint(1,sum_count) #获取一个随机数
        filepath = path+str(n)+'.jpg'
        ctypes.windll.user32.SystemParametersInfoW(20, 0, filepath, 0)#window系统设定壁纸
        time.sleep(60)#刷新壁纸的时间，单位秒